
import plugin from '../../../lib/plugins/plugin.js'
import { existplayer } from './xiuxian.js'
import fs from "node:fs"

/**
 * 修仙设置
 */
export class AdminSuper extends plugin {
    constructor() {
        super({
            name: "Yunzai_Bot_AdminSuper",
            dsc: "修仙设置",
            event: "message",
            priority: 100,
            rule: [
                {
                    reg: "^#解封.*$",//解除readis时间动作锁
                    fnc: "relieve",
                },
                {
                    reg: "^#解除所有$",//解除readis时间动作锁
                    fnc: "Allrelieve",
                },
                {
                    reg: "^#打落凡间.*$",//直接仙人跌落人界，永不能返回
                    fnc: "Knockdown",
                },
                {
                    reg: "^#备份存档$",
                    fnc: "Backups",
                }
            ],
        });
    }
    async Allrelieve(e){
        if (!e.isMaster) {
            e.reply("只有主人可以执行操作");
            return;
        }
        e.reply("开始行动！");
        let playerList = [];
        let files = fs
            .readdirSync("./plugins/xiuxian-emulator-plugin/resources/data/xiuxian_player")
            .filter((file) => file.endsWith(".json"));
        for (let file of files) {
            file = file.replace(".json", "");
            playerList.push(file);
        }
        for (let player_id of playerList) {
            //清除游戏状态
            await redis.set("xiuxian:player:" + player_id + ":game_action",1);
            let action = await redis.get("xiuxian:player:" + player_id + ":action");
            action = JSON.parse(action);
            //不为空，存在动作
            if (action != null) {
            e.reply(player_id+"完成！");
            await redis.del("xiuxian:player:" + player_id + ":action");
            let arr = action; 
            arr.is_jiesuan = 1;//结算状态
            arr.shutup=1;//闭关状态
            arr.working=1;//劳动状态
            arr.power_up = 1;//渡劫状态
            arr.Place_action = 1;//秘境
            arr.Place_actionplus = 1;//沉迷状态
            arr.end_time = new Date().getTime();//结束的时间也修改为当前时间
            delete arr.group_id;//结算完去除group_id
            await redis.set("xiuxian:player:" + player_id + ":action", JSON.stringify(arr));
            }
        }
    }

    
  

    async relieve(e) {
        //主人判断
        if (!e.isMaster) {
            e.reply("只有主人可以执行操作");
            return;
        }
        //没有at信息直接返回,不执行
        let isat = e.message.some((item) => item.type === "at");
        if (!isat) {
            return;
        }
        //获取at信息
        let atItem = e.message.filter((item) => item.type === "at");
        //对方qq
        let qq = atItem[0].qq;
        //检查存档
        let ifexistplay = await existplayer(qq);
        if (!ifexistplay) {
            e.reply("没存档解封个锤子!");
            return;
        }
        
        //清除游戏状态
        await redis.set("xiuxian:player:" + qq + ":game_action",1);
        //查询redis中的人物动作
        let action = await redis.get("xiuxian:player:" + qq + ":action");
        action = JSON.parse(action);
        //不为空，有状态
        if (action != null) {
            //把状态都关了
            let arr = action; 
            arr.is_jiesuan = 1;//结算状态
            arr.shutup=1;//闭关状态
            arr.working=1;//劳动状态
            arr.power_up = 1;//渡劫状态
            arr.Place_action = 1;//秘境
            arr.Place_actionplus = 1;//沉迷状态
            arr.end_time = new Date().getTime();//结束的时间也修改为当前时间
            delete arr.group_id;//结算完去除group_id
            await redis.set("xiuxian:player:" + qq + ":action", JSON.stringify(arr));
            e.reply("已解除！");
            return;
        }
        //是空的
        e.reply("不需要解除！");
        return;
    }

    async Knockdown(e){
        //主人判断
        if (!e.isMaster) {
            e.reply("只有主人可以执行操作");
            return;
        }
        //没有at信息直接返回,不执行
        let isat = e.message.some((item) => item.type === "at");
        if (!isat) {
            return;
        }
        //获取at信息
        let atItem = e.message.filter((item) => item.type === "at");
        //对方qq
        let qq = atItem[0].qq;
        //检查存档
        let ifexistplay = await existplayer(qq);
        if (!ifexistplay) {
            e.reply("没存档你打个锤子！");
            return;
        }
        //得到对方的仙门

        let power_place = await redis.get("xiuxian:player:" + qq + ":power_place");
        if(power_place==0){
            await redis.set("xiuxian:player:" + qq + ":power_place", 1);
            e.reply("已打落凡间！");
        }
        else{
            e.reply("此人未有成仙资格！");
        }
        
    }


}